package com.itheima.manager.controller;

import com.itheima.manager.domain.Student;
import com.itheima.manager.service.StudentService;

import java.util.ArrayList;
import java.util.Scanner;

public abstract class BaseStudentControll {

    Scanner sc = new Scanner(System.in);

    private StudentService stuser = new StudentService();

    public void start() {

        while (true) {
            System.out.println("-------------欢迎来到学生管理系统----------------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查看学生");
            System.out.println("5.退出系统");

            String choice = sc.next();

            switch (choice) {
                case "1":
                    addStudent();
                    break;
                case "2":
                    deleteStudent();
                    break;
                case "3":
                    updateStudent();
                    break;
                case "4":
                    //System.out.println("查看学生");
                    findAllAStudent();
                    break;
                case "5":
                    System.out.println("感谢您的使用");
                    return;
                default:
                    break;



            }


        }


    }

    //修改学生方法
    private void updateStudent() {
        ArrayList<Student>  list = stuser.findAllStudent();
        if (list==null){
            System.out.println("查无信息,请添加");
            return;
        }
        System.out.println("请输入需要修改的学生学号");
        String updateId= inputStudentId(true);

        Student newStu = inputStudentInfo(updateId);
        stuser.updateStudent(updateId,newStu);
        System.out.println("修改成功");
    }

    public void deleteStudent() {

        ArrayList<Student>  list = stuser.findAllStudent();
        if (list==null){
            System.out.println("查无信息,请添加");
            return;
        }

        System.out.println("请输入学号: ");
        String sSid = sc.next();

        boolean result = stuser.deleteStudent(sSid);

        if(result){
            System.out.println("删除成功!");
        }else{
            System.out.println("删除失败!");
        }

    }

    public void findAllAStudent() {

        ArrayList<Student>  list = stuser.findAllStudent();
        if (list == null){
            System.out.println("查无信息,请添加");
            return;
        }

        System.out.println("学号\t姓名\t年龄\t出生日期");
        list.forEach(stu -> System.out.println(stu));
    }

    private void addStudent() {
        System.out.println("请输入学生学号");
        String id = inputStudentId(false);

        Student stu = inputStudentInfo(id);
        stuser.addStudent(stu);
        System.out.println("添加成功");

    }
    //抽取一个校验学生学号的方法并设置一个开关
    public String inputStudentId(boolean flag){
        String id;
        while (true){
            id = sc.next();
            boolean exists=stuser.isExists(id);
            if(exists==flag){
                break;
            }else {
                System.out.println("学好不可用，请重新输入");
            }
        }
        return id;
    }
    //抽取一个键盘录入学生信息的方法
    public abstract Student inputStudentInfo(String id);
}
